package com.rockxin.cat.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.rockxin.cat.R;
import com.rockxin.cat.adapter.RedPacketAdapter;
import com.rockxin.cat.base.BaseFragment;
import com.rockxin.cat.base.Constant;
import com.rockxin.cat.base.User;
import com.rockxin.cat.bean.RedPacketBean;
import com.rockxin.cat.customview.NoScrollViewPager;
import com.rockxin.cat.event.RedPacketTypeEvent;
import com.rockxin.cat.network.CatNetWork;
import com.rockxin.cat.network.HttpResult;
import com.rockxin.cat.utils.SPUtils;

import org.simple.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import rx.Subscriber;

/**
 * A simple {@link Fragment} subclass.
 * <p>
 * 红包使用情况 未使用、已使用、已过期
 */
public class RedPacketFragment extends BaseFragment {
    private static final String TAG = "RedPacketFragment";
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private Unbinder bind;
    private int type;   //1=>'我的加息券列表',2=>'我的节日红包列表',3=>'我的8888现金红包活动列表'

    private RedPacketAdapter mRedPacketAdapter;
    private List<RedPacketBean> mRedPacketBeanList;
    private TextView unUsed;
    private TextView haveUse;
    private TextView haveExpired;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            type = getArguments().getInt("type");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View mView = inflater.inflate(R.layout.fragment_red_packet, container, false);
        bind = ButterKnife.bind(this, mView);
        initView();
        return mView;
    }

    public RedPacketFragment() {
    }

    public static RedPacketFragment newInstance(int type) {
        Bundle args = new Bundle();
        args.putInt("type", type);
        RedPacketFragment fragment = new RedPacketFragment();
        fragment.setArguments(args);
        return fragment;
    }

    private void initView() {
        mRedPacketBeanList = new ArrayList<>();
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        mRedPacketAdapter = new RedPacketAdapter(mRedPacketBeanList);
        recyclerView.setAdapter(mRedPacketAdapter);
        mRedPacketAdapter.setType(type);
        setHeader();
        getNetData(1);

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        bind.unbind();
    }

    /**
     * 设置为初始状态
     */
    private void setDefaultState() {
        unUsed.setTextColor(ContextCompat.getColor(mContext, R.color.colorTextColor));
        unUsed.setBackgroundColor(ContextCompat.getColor(mContext, R.color.colorWhite));
        haveUse.setTextColor(ContextCompat.getColor(mContext, R.color.colorTextColor));
        haveUse.setBackgroundColor(ContextCompat.getColor(mContext, R.color.colorWhite));
        haveExpired.setTextColor(ContextCompat.getColor(mContext, R.color.colorTextColor));
        haveExpired.setBackgroundColor(ContextCompat.getColor(mContext, R.color.colorWhite));
    }

    public void setHeader() {
        final View header = View.inflate(mContext, R.layout.fragment_red_packet_header, null);
        unUsed = header.findViewById(R.id.unused);
        haveUse = header.findViewById(R.id.haveUse);
        haveExpired = header.findViewById(R.id.haveExpired);

        unUsed.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setDefaultState();
                unUsed.setTextColor(ContextCompat.getColor(mContext, R.color.colorWhite));
                unUsed.setBackgroundColor(ContextCompat.getColor(mContext, R.color.colorMoney));
                getNetData(1);
            }
        });
        haveUse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setDefaultState();
                haveUse.setTextColor(ContextCompat.getColor(mContext, R.color.colorWhite));
                haveUse.setBackgroundColor(ContextCompat.getColor(mContext, R.color.colorMoney));
                getNetData(2);
            }
        });

        haveExpired.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setDefaultState();
                haveExpired.setTextColor(ContextCompat.getColor(mContext, R.color.colorWhite));
                haveExpired.setBackgroundColor(ContextCompat.getColor(mContext, R.color.colorMoney));
                getNetData(3);
            }
        });

        mRedPacketAdapter.setHeaderView(header);
    }

    private void getNetData(int type2) {
        SPUtils spUtils = new SPUtils("Guide");
        String token = spUtils.getString(Constant.PREFERENCES.TOKEN);

        CatNetWork catNetWork = new CatNetWork();
        catNetWork.myVoucher(type, type2, token, 1, new rx.Subscriber<HttpResult<List<RedPacketBean>>>() {
            @Override
            public void onCompleted() {
                com.orhanobut.logger.Logger.e(TAG + "myVoucher---->onCompleted");
            }

            @Override
            public void onError(Throwable e) {
                com.orhanobut.logger.Logger.e(TAG + "myVoucher---->onError----->\n" + e.getMessage());
                e.printStackTrace();
            }

            @Override
            public void onNext(final HttpResult<List<RedPacketBean>> listHttpResult) {
                mRedPacketBeanList.clear();
                if (listHttpResult.getData() != null) {

                    mRedPacketBeanList.addAll(listHttpResult.getData());
                }
                mRedPacketAdapter.notifyDataSetChanged();
            }
        });
    }
}
